//
//  LoginViewController.m
//  DaHaiDouBan
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 Wang Dahai. All rights reserved.
//

#import "LoginViewController.h"
#import "zhuceViewController.h"
@interface LoginViewController ()

@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *passwordLabel;
@property (nonatomic, retain) UITextField *nameTextField;
@property (nonatomic, retain) UITextField *passwordTextField;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIButton *zhuceButton;

@end

@implementation LoginViewController
- (void)dealloc
{
    [_nameLabel release];
    [_passwordLabel release];
    [_nameTextField release];
    [_passwordTextField release];
    [_loginButton release];
    [_zhuceButton release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 128, 100, 40)];
    self.nameLabel.text = @"用户名:";
    [self.view addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 128, 200, 40)];
    self.nameTextField.placeholder = @"请输入用户名";
    self.nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.nameTextField];
    [_nameTextField release];
    
    self.passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 188, 100, 40)];
    self.passwordLabel.text = @"密码:";
    [self.view addSubview:self.passwordLabel];
    [_passwordLabel release];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 188, 200, 40)];
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.passwordTextField];
    [_passwordTextField release];
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.loginButton.frame = CGRectMake(30, 280, 150, 49);
    [self.view addSubview:self.loginButton];
    [self.loginButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.loginButton setBackgroundImage:[UIImage imageNamed:@"111"] forState:UIControlStateNormal];
    
    
    
    self.zhuceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.zhuceButton.frame = CGRectMake(210, 280, 150, 49);
     [self.view addSubview:self.zhuceButton];
    [self.zhuceButton addTarget:self action:@selector(zhuceButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.zhuceButton setBackgroundImage:[UIImage imageNamed:@"222"] forState:UIControlStateNormal];
    
    
    
}

-(void)loginButtonAction:(UIButton *)loginbtn
{
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    if ([self.nameTextField.text isEqualToString:name] && [self.passwordTextField.text isEqualToString:password]) {
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"是的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];

        }];
        
        [alertVC addAction:action1];
        [alertVC addAction:action2];
        
        [self presentViewController:alertVC animated:YES completion:^{
            
            
        }];

    }else{
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"用户名或密码不正确" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"是的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        
        [alertVC addAction:action1];
        [alertVC addAction:action2];
        
        [self presentViewController:alertVC animated:YES completion:^{
            
            
        }];
        
        
    }
    
    
}

-(void)zhuceButtonAction:(UIButton *)zhuceBtn
{
    zhuceViewController *zhuceVC = [[zhuceViewController alloc] init];
    
    [self.navigationController pushViewController:zhuceVC animated:YES];
    
    [zhuceVC release];
    
    
}
-(void)leftBarButtonAction:(UIBarButtonItem *)leftbtn
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
